package com.tulele.vtool.service.impl;

import com.github.binarywang.wxpay.bean.entpay.EntPayRequest;
import com.github.binarywang.wxpay.bean.request.BaseWxPayRequest;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tulele.vtool.mapper.UserWithdrawLogInfoMapper;
import com.tulele.vtool.model.UserInfo;
import com.tulele.vtool.model.UserWithdrawLogInfo;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.IUserInfoService;
import com.tulele.vtool.service.IUserWithdrawLogInfoService;
import com.tulele.vtool.task.async.EnterprisePayTask;
import com.tulele.vtool.utils.DateUtil;
import com.tulele.vtool.utils.SysConstant;
import com.tulele.vtool.utils.ToolUtil;
import com.tulele.vtool.wechat.WechatAccountConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class UserWithdrawLogInfoServiceImpl implements IUserWithdrawLogInfoService {

    @Autowired
    private UserWithdrawLogInfoMapper userWithdrawLogInfoMapper;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private EnterprisePayTask enterprisePayTask;

    @Autowired
    private WechatAccountConfig wechatAccountConfig;

    @Override
    public UserWithdrawLogInfo getUserWithdrawDesc1(Integer userId) {
        UserWithdrawLogInfo userWithdrawLogInfo = userWithdrawLogInfoMapper.queryUserWithdrawDesc1(userId);

        return userWithdrawLogInfo;
    }

    @Override
    public PageInfo<UserWithdrawLogInfo> getUserWithdrawPageInfo(Map<String, Object> queryMap) {
        int pageNum = (int)queryMap.get("pageNum");
        int pageSize = (int)queryMap.get("pageSize");

        int userId = (int)queryMap.get("userId");

        PageHelper.startPage(pageNum, pageSize);
        List<UserWithdrawLogInfo> lotteryInfoList = userWithdrawLogInfoMapper.queryUserWithdrawInfo(userId);

        return new PageInfo<UserWithdrawLogInfo>(lotteryInfoList);
    }

    @Override
    public int applyWithdrawCommission(UserWithdrawLogInfo userWithdrawLogInfo) {
        int userId = userWithdrawLogInfo.getUserId();

        //用户申请提现的金额
        double amount = userWithdrawLogInfo.getAmount();

        //获取用户当前的账户金额
        Double beforeBalance = Optional.ofNullable(userInfoService.getUserAccountBalance(userId)).orElse(0d);

        if (amount > beforeBalance) {

            return ReturnCode.WITHDRAW_SHORT_OF_MONEY;

        }

        if (amount <= 100) {

            userWithdrawLogInfo.setOutBizNo(ToolUtil.generateOrderId());

        }

        userWithdrawLogInfo.setBeforeBalance(beforeBalance.intValue());

        int afterBalance = 0;
        userWithdrawLogInfo.setAfterBalance(afterBalance);

        userWithdrawLogInfo.setApplyType(SysConstant.WITHDRAW_COMMISSION_APPLY_TYPE);
        userWithdrawLogInfo.setRemark("申请提现金额："+ amount +"元");
        userWithdrawLogInfo.setCreateTime(DateUtil.getCurDate());

        userWithdrawLogInfoMapper.addWithdrawInfo(userWithdrawLogInfo);

        //申请提现后更新用户金币余额
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userId);
        userInfo.setAccountBalance(0d);
        userInfo.setModifyTime(DateUtil.getCurDate());
        userInfoService.modifyUserAccountBalance(userInfo);

        if (amount <= 100) {

            EntPayRequest request = new EntPayRequest();
            request.setPartnerTradeNo(userWithdrawLogInfo.getOutBizNo());
            request.setCheckName("NO_CHECK");
            request.setAmount(BaseWxPayRequest.yuanToFen(String.valueOf(amount)));
            request.setDescription("佣金付款");

            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            request.setSpbillCreateIp(ToolUtil.getIpAddress(attributes.getRequest()));

            String openId = userInfoService.getUserOpenId(userId);
            request.setOpenid(openId);

            enterprisePayTask.entPay(userWithdrawLogInfo.getId(), request);

        }


        return 0;
    }

}
